import java.util.ArrayList;
import java.util.Random;

import javafx.scene.layout.AnchorPane;
import javafx.scene.text.Text;



public class Deck extends AnchorPane{
	private  ArrayList<Card> cards = new ArrayList<Card>();
	private Game delegator;
	private Text remains=new Text();
	int delta;
	
	public Deck(){
		this.init();
	}
	
	public void setDelegator(Game ob){
		delegator = ob;
	}
	
	/**
	 * @brief 产生一种堆叠的视觉效果
	 */
	public void init(){
	
		this.getChildren().add(remains);
		remains.setX(40);
		remains.setY(160);
		remains.setText("剩余："+cards.size());
		
		for(int i=0;i<=10;i++){
			for(int j=1;j<=6;j++){
				Card temp = new Card(i,j);
				
				temp.turnBack();
				cards.add(temp);
				this.getChildren().add(temp);
				
				delta = j*10+i;
				temp.setX(delta*0.1);
				temp.setY(-delta*0.12);
			}
		}
	}
	
	public Card drawACard(){

		if(this.size()>0){
			Random r = new Random();
			int index = r.nextInt(this.size());
			Card ret = cards.get(index);

			this.getChildren().remove(ret);
			cards.remove(ret);
			remains.setText(cards.size()+"");
			
			return ret;
		}
		else{
			delegator.end();
			return null;
		}

	}
	
	/**
	 * @brief 洗牌既是回复初始的状态
	 */
	public void reset(){
		this.getChildren().clear();
		cards.clear();
		this.init();
	}
	
	public int size(){
		return cards.size();
	}
	
	
}
